package com.spring.showcase.controllers;

import java.util.concurrent.Callable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.spring.showcase.domain.User;

@Controller
@RequestMapping(value = "/async")
public class AsyncController {

    public static DeferredResult<User> result = new DeferredResult<User>();

    @RequestMapping("/callable")
    @ResponseBody
    public Callable<User> processCall() {
        return new Callable<User>() {
            @Override
            public User call() throws Exception {
                Thread.sleep(3000);
                return new User("call");
            }
        };
    }

    @RequestMapping("/defer")
    @ResponseBody
    public DeferredResult<User> getDeferredResult() {
        result.onCompletion(new Runnable() {
            @Override
            public void run() {
                result = new DeferredResult<User>();
            }
        });
        return result;
    }

    //    @Scheduled(fixedRate = 5000)
    //    public void processQueues() {
    //        result.setResult(new User("defer"));
    //    }

}
